import Mock from 'mockjs'

const data=Mock.mock({
    "user":[{
        "username":"admin",
        "password":"1234",
        "name":'张三'
    },{
        "username":"user",
        "password":"1234",
        "name":"李四"
    }]
})

let codes=null
Mock.mock('/api/getcode','post',()=>{
    codes=Math.floor(Math.random()*(9999-1000))+1000
    console.log(codes)
    return({
        code:200,
        data:codes
    })
})
Mock.mock('/api/login','post',(req,res)=>{
    const {username,password,code}=JSON.parse(req.body) 
    console.log(req.body)
    const isLogin=data.user.find(item=>item.username===username&&item.password===password)
    if(isLogin){
        if(codes===code*1){
            return({
                code:200,
                mes:'登录成功',
                data:isLogin.name
            })
        }else{
            return({
                code:303,
                mes:'验证码错误'
            })
        }
    }else{
        return({
            code:400,
            mes:'账号密码有误'
        })
    }
})
